// pages/userCenter/bidorderDetail/bidorderDetail.js
var app = getApp()
var util = require("../../../utils/util.js");
var config = require("../../../utils/config.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    screen: app.data.screen,
    hidden: 2,
    message: [],
    ststus: '',
    money: '',
    content: '',
    imgview: '',
    imgurl: '',
    id:'',
    bid_id: ''
  },

  portrait() {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        that.setData({
          imgview: res.tempFilePaths[0], //返回的图片路径
        });
        wx.uploadFile({
          url: config.upLoad, // 仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'imgurl',
          // formData: {
          //   imgurl: 'img12'
          // },
          success: function (res1) {
            var jsonString = res1.data;
            if (typeof (jsonString) == 'string') {
              jsonString = jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}") + 1);
              res1.data = JSON.parse(jsonString);
            }
            that.setData({
              imgurl: res1.data.data.imgurl, //返回的图片路径
            });
      }
        })
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      id: options.id,
      status: options.status,
      money: options.money,
      bid_id: options.bid_id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('455555555555')
    console.log("http://tmp/wx2647b65c2027ecaf.o6zAJs1EduJ26VChZyPCfpkwQvA8.LVq2OEsklL5cbed69940670ed5050bd436a19fa0c35a.jpg".indexOf('http'))
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          that.request();

        } else {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      }
    })
    if (that.data.message.length <= 0) {
      that.request();
    }
  },

  // 防止重复点击
  waiton() {
    this.setData({
      isDisabled: true
    })
    setTimeout(() => {
      this.setData({
        isDisabled: false
      })
    }, 2000);
  },
 
  // 确定投标
  bidSure(e) {
    console.log(e)
    var formid = e.detail.formId;
    this.waiton();
    if (e.currentTarget.dataset.btn == 0) {
      var flag = 1;      
    } else {
      var flag = 2;            
    }
    util.getOpenID((e) => {
        var parameters = {
          openid: e.openid,
          flag: flag,
          id: this.data.id,
        }    
      var parameters1 = {
        openid: e.openid,
        formid: formid,
      }
      // 收集模板信息
      util.collectRequest(config.collectUser, parameters1, (res) => {
        // console.log(res)
      });
      // 修改信息
      util.postRequest(config.bidSure, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {

          util.showModal2(res.msg, function (res) {
            wx.navigateTo({
              url: '/pages/userCenter/bidOrder/bidOrder'
            })
          })
        } else if (res.code == 201) {
          util.showModal2(res.msg, '')
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  },
  // 提交
  submitTo(e) {
    console.log(e)
    var formid = e.detail.formId;
    this.waiton();
    util.getOpenID((e) => {
      if (this.data.money == 2 || !this.data.money) {
        var parameters = {
          openid: e.openid,
          imgurl: this.data.imgurl,
          bond_id: this.data.bid_id,
        }
      } else {
        var parameters = {
          openid: e.openid,
          imgurl: this.data.imgurl,
          id: this.data.id,
        }
      }    
      var parameters1 = {
        openid: e.openid,
        formid: formid,
      }
      // 收集模板信息
      util.collectRequest(config.collectUser, parameters1, (res) => {
        // console.log(res)
      });
      // 修改信息
      util.postRequest(config.bidMoney, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {
          util.showModal2(res.msg, function (res) {
            wx.navigateTo({
              url: '/pages/userCenter/bidOrder/bidOrder'
            })
          })
        } else if (res.code == 201) {
          util.showModal2(res.msg, function (res) {
            wx.navigateTo({
              url: '/pages/userCenter/bidOrder/bidOrder'
            })
          })
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  },
  //  网络请求
  request() {
    util.getOpenID((e) => {
      var parameters = {
        openid: e.openid,
        id: this.data.id
      }
      util.postRequest(config.bidDetail, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {
          this.setData({
            hidden: 1,
            message: res.data.table,
            content: res.data.message
          })
        } else if (res.code == 201) {
          wx.showToast({ title: '请求失败!', icon: 'none' })
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  },
  // 点击预览
  previewImg(e) {
    var index = e.currentTarget.dataset.index;
    var imgArr = [];
    imgArr.push(this.data.imgurl[index]);
    wx.previewImage({
      current: this.data.imgurl[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success(res) {

      },
      fail(res) { },
      complete(res) { },
    })
  },
})